//
//
//
#include <string.h>
#include <unistd.h>
#include "file/ed_fop.h"
#include <dirent.h>

jint StringAppendFile(JNIEnv *env, jobject jclazz, jstring path, jstring content)
{
    const char *filePath = (*env)->GetStringUTFChars(env, path, JNI_FALSE);
    FILE *target = fopen(filePath, "a+");
    if (target == NULL)
    {
        return -1;
    } else
    {
        const char *contentStr = (*env)->GetStringUTFChars(env, content, JNI_FALSE);
        size_t length = strlen(contentStr);
        for (int i = 0; i < length; i++)
        {
            fputc(contentStr[i], target);
        }
        fclose(target);
        return 1;
    }
}

void HandleError(FILE *f);

jstring ReadStringFile(JNIEnv *env, jobject clazz, jstring path)
{
    const char *filePath = (*env)->GetStringUTFChars(env, path, JNI_FALSE);
    FILE *target = fopen(filePath, "r");
    if (target == NULL)
    {
        return (*env)->NewStringUTF(env, "NULL");
    } else
    {
        long strSize;
        fseek(target, 0, SEEK_END);
        strSize = ftell(target);
        rewind(target);

        char buffer[512];
        char targetStr[strSize];

        memset(targetStr, 0, sizeof(targetStr));

        char *p;
        while ((p = fgets(buffer, 512, target)) != NULL)
        {
            if (ferror(target))
            {
                HandleError(target);
                return (*env)->NewStringUTF(env, "Error occurred");
            } else
            {
                strcat(targetStr, p);
            }
        }
        fclose(target);
        return (*env)->NewStringUTF(env, targetStr);
    }
}

void HandleError(FILE *f)
{
    if (f != NULL)
    {
        clearerr(f);
        fclose(f);
    }
}

jint ByteArray2File(JNIEnv *env, jobject jclazz, jstring path, jbyteArray content)
{
    const char *filePath = (*env)->GetStringUTFChars(env, path, JNI_FALSE);
    FILE *target = fopen(filePath, "a+");
    if (target == NULL)
    {
        return -1;
    } else
    {
        const char *contentStr = (*env)->GetStringUTFChars(env, content, JNI_FALSE);
        int length = strlen(contentStr);
        for (int i = 0; i < length; i++)
        {
            fputc(contentStr[i], target);
        }
        fclose(target);
        return 1;
    }
}

/**
 * File_Delete_Dir--删除文件目录
 */
jint _delete_dir(JNIEnv *env, jobject jclazz, jstring path)
{
    if (path == NULL)
    {
        return -1;
    }
    const char *dirName = (*env)->GetStringUTFChars(env, path, JNI_FALSE);

    if (dirName == NULL)
    {
        return -1;
    }
    DIR *dp = NULL;
    DIR *dpin = NULL;
    char *pathname = (char *) malloc(256);
    memset(pathname, 0, 256);
    struct dirent *dirp;
    dp = opendir(dirName);
    if (dp == NULL)
    {
        return -1;
    }
    while ((dirp = readdir(dp)) != NULL)
    {
        if (strcmp(dirp->d_name, "..") == 0 || strcmp(dirp->d_name, ".") == 0)
            continue;
        strcpy(pathname, dirName);
        strcat(pathname, "/");
        strcat(pathname, dirp->d_name);
        dpin = opendir(pathname);
        if (dpin != NULL)
        {
            closedir(dpin);
            dpin = NULL;
            _delete_dir(env, jclazz, (*env)->NewStringUTF(env, pathname));
        } else
        {
            remove(pathname);
        }
    }
    rmdir(dirName);
    free(pathname);
    pathname = NULL;
    dirp = NULL;
    return 0;
}


